﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.Web.UI.Controls;
using ZX.DBModule;
using ZX.BLL;
using ZX.EHR.Library;
using ZX.DAL.DBExtend;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using ZX.BLL.Extend;
using ZX.Common;

namespace ZX.WebApp.EHR
{
    public partial class DatPostAjax : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string flag = this.GetStringFromQueryString("flag", "");
            switch (flag.ToLower())
            {
                case "isdelskillcategory":
                    IsDelSkillCategory();
                    break;
                case "getchildcategorynextseq":
                    GetChildCategoryNextSeq();
                    break;
                case "deleteskill":
                    DeleteSkill();
                    break;
                case "isdelsystasktype":
                    IsDelSysTaskType();
                    break;
                case "getchildsystasktypenextseq":
                    GetChildSysTaskTypeNextSeq();
                    break;
                case "getfieldbytable":
                    GetFieldbyTable();
                    break;

            }
        }

        #region 判断是否可以删除分类
        /// <summary>
        /// 判断是否可以删除分类
        /// </summary>
        public void IsDelSkillCategory()
        {
            string id = this.GetStringFromQueryString("id", "");
            try
            {
                //是否有下级分类
                string sql = string.Format("select id from datSkillCategory where ParentID='{0}'", id);
                //是否有技能引用了分类
                string skillsql = string.Format("select id from datSkill where Category='{0}'", id);
                DbSql dbsql = new DbSql();
                DataTable dt = dbsql.ExecuteQuery(sql);
                DataTable dtskill = dbsql.ExecuteQuery(skillsql);
                if (dt.Rows.Count > 0)
                {
                    HttpContext.Current.Response.Write("2");//有下级分类
                }
                else if (dtskill.Rows.Count > 0)
                {
                    HttpContext.Current.Response.Write("3");//有技能引用了分类
                }
                else if (dbsql.ExecuteSql(string.Format("Delete from datSkillCategory where id='{0}'", id)) > 0)
                {
                    HttpContext.Current.Response.Write("1");//删除成功
                }
                else
                {
                    HttpContext.Current.Response.Write("0");//删除失败
                }
            }
            catch
            {
                HttpContext.Current.Response.Write("-1");//出错
            }
        }
        #endregion

        #region 自动生成下级分类编码
        /// <summary>
        /// 自动生成下级分类编码
        /// </summary>
        public void GetChildCategoryNextSeq()
        {
            string ParentID = this.GetStringFromQueryString("ParentID", "");
            string sql = string.Format("select IsNULL(MAX(Seq),'001') from datSkillCategory where ParentID='{0}'", ParentID);
            string sql1 = string.Format("select seq from datSkillCategory where ID='{0}'", ParentID);
            DbSql dbsql = new DbSql();
            DataTable dt = dbsql.ExecuteQuery(sql);
            DataTable dt1 = dbsql.ExecuteQuery(sql1);
            int nextSeq = 1;
            string path = "", nextpath = "";
            if (dt.Rows.Count == 1)
            {
                path = dt.Rows[0][0].ToString();
                string[] ss = path.Split('-');
                if (ss.Length > 1)
                {
                    Int32.TryParse(ss[ss.Length - 1], out nextSeq);
                    nextSeq++;

                    for (int i = 0; i < ss.Length - 1; i++)
                    {
                        nextpath += ss[i] + "-";
                    }
                    for (int i = 0; i < 3 - nextSeq.ToString().Length; i++)
                    {
                        nextpath += "0";
                    }
                    nextpath += nextSeq.ToString();
                }
                else
                {
                    nextpath = dt1.Rows[0][0].ToString() + "-" + path;
                }
            }
            HttpContext.Current.Response.Write(nextpath);
        }
        #endregion

        #region 技能删除
        /// <summary>
        /// 技能删除
        /// </summary>
        public void DeleteSkill()
        {
            try
            {
                string skillID = this.GetStringFromQueryString("json", "").Replace("[@@]", "\\");

                if (skillID == string.Empty)
                {
                    HttpContext.Current.Response.Write("-1"); //ID为空
                    return;
                }
                skillID = "'" + skillID.Replace(",", "','") + "'";
                BaseEntity instance = null;
                ZX.BLL.Extend.BaseBussnies baseBll = null;
                string condition = "";
                bool isDelete = true;
                //定义引用表
                string[] tableName = { "DatPostSkillRequire" };
                //先查询是否被引用了，如果有引用则不能删除，给提示
                foreach (string s in tableName)
                {
                    instance = ZX.Common.Globals.getEntity(s) as BaseEntity;
                    baseBll = new ZX.BLL.Extend.BaseBussnies(instance);
                    condition = string.Format("skillID in ({0})", skillID);
                    isDelete = (baseBll.GetEntityByCondition(condition) == null);
                    if (!isDelete) break;
                }
                if (isDelete)
                {
                    //删除各引用表数据
                    foreach (string s in tableName)
                    {
                        instance = ZX.Common.Globals.getEntity(s) as BaseEntity;
                        condition = string.Format("skillID  in ({0})", skillID);
                        baseBll = new ZX.BLL.Extend.BaseBussnies(instance);
                        baseBll.delete(condition);
                    }
                    //删除班次子表
                    instance = ZX.Common.Globals.getEntity("datSkillLevel") as BaseEntity;
                    condition = string.Format("skillID  in ({0})", skillID);
                    baseBll = new ZX.BLL.Extend.BaseBussnies(instance);
                    baseBll.delete(condition);
                    //删除主表
                    instance = ZX.Common.Globals.getEntity("datSkill") as BaseEntity;
                    condition = string.Format("ID  in ({0})", skillID);
                    baseBll = new ZX.BLL.Extend.BaseBussnies(instance);
                    baseBll.delete(condition);
                    HttpContext.Current.Response.Write("1");
                }
                else
                {
                    HttpContext.Current.Response.Write("0");  //有引用则不能删除
                    return;
                }
            }
            catch (Exception e)
            {
                HttpContext.Current.Response.Write("e" + e.Message);
            }
        }
        #endregion

        #region 自动生成预警下级分类编码
        /// <summary>
        /// 自动生成下级分类编码
        /// </summary>
        public void GetChildSysTaskTypeNextSeq()
        {
            string ParentID = this.GetStringFromQueryString("ParentID", "");
            string sql = string.Format("select IsNULL(MAX(Seq),'001') from SysTaskType where ParentID='{0}'", ParentID);
            string sql1 = string.Format("select seq from SysTaskType where ID='{0}'", ParentID);
            DbSql dbsql = new DbSql();
            DataTable dt = dbsql.ExecuteQuery(sql);
            DataTable dt1 = dbsql.ExecuteQuery(sql1);
            int nextSeq = 1;
            string path = "", nextpath = "";
            if (dt.Rows.Count == 1)
            {
                path = dt.Rows[0][0].ToString();
                string[] ss = path.Split('-');
                if (ss.Length > 1)
                {
                    Int32.TryParse(ss[ss.Length - 1], out nextSeq);
                    nextSeq++;

                    for (int i = 0; i < ss.Length - 1; i++)
                    {
                        nextpath += ss[i] + "-";
                    }
                    for (int i = 0; i < 3 - nextSeq.ToString().Length; i++)
                    {
                        nextpath += "0";
                    }
                    nextpath += nextSeq.ToString();
                }
                else
                {
                    nextpath = dt1.Rows[0][0].ToString() + "-" + path;
                }
            }
            HttpContext.Current.Response.Write(nextpath);
        }
        #endregion

        #region 判断是否可以删除预警分类
        /// <summary>
        /// 判断是否可以删除预警分类
        /// </summary>
        public void IsDelSysTaskType()
        {
            string id = this.GetStringFromQueryString("id", "");
            try
            {
                //根目录不能删除
                string sqlroot = string.Format("select id from SysTaskType where ID='{0}' and ParentID='00000000-0000-0000-0000-000000000000'", id);
                //是否有下级分类
                string sql = string.Format("select id from SysTaskType where ParentID='{0}'", id);
                //是否有预警信息引用了分类
                string skillsql = string.Format("select id from SysTask where TaskTypeID='{0}'", id);
                DbSql dbsql = new DbSql();
                DataTable dt = dbsql.ExecuteQuery(sql);
                DataTable dtskill = dbsql.ExecuteQuery(skillsql);
                if (dbsql.ExecuteQuery(sqlroot).Rows.Count > 0)
                {
                    HttpContext.Current.Response.Write("4");//根目录不能删除
                }
                else if (dt.Rows.Count > 0)
                {
                    HttpContext.Current.Response.Write("2");//有下级分类
                }
                else if (dtskill.Rows.Count > 0)
                {
                    HttpContext.Current.Response.Write("3");//有技能引用了分类
                }
                else if (dbsql.ExecuteSql(string.Format("Delete from SysTaskType where id='{0}'", id)) > 0)
                {
                    HttpContext.Current.Response.Write("1");//删除成功
                }
                else
                {
                    HttpContext.Current.Response.Write("0");//删除失败
                }
            }
            catch
            {
                HttpContext.Current.Response.Write("-1");//出错
            }
        }
        #endregion

        #region 根据实体名取得列表字段
        /// <summary>
        /// 根据实体名取得列表字段,返回JSON
        /// </summary>
        public void GetFieldbyTable()
        {
            string json = string.Empty;
            string table = this.GetStringFromQueryString("table", "");
            string sql = @"select Field FieldEName,THCaption FieldCName,iOrder FieldSeq from Set_DataListTemplate 
            where EntityName ='{0}' and Field !='ID' and THCaption !='ID' order by iOrder";
            BaseBussnies bll = new BaseBussnies();
            System.Data.DataTable dt = bll.GetDataBySqlComm(string.Format(sql, table));
            json = Globals.DataTableToCJson(dt);
            Response.ContentType = "text/plain";
            Response.Write(json);
            Response.End();

        }
        #endregion
    }
}